//
//  XNStatusRetweetedView.swift
//  weibo
//
//  Created by 王向宁 on 16/5/25.
//  Copyright © 2016年 王向宁. All rights reserved.
//转发微博视图

import UIKit
import SnapKit

class XNStatusRetweetedView: UIView {
    
    var bottomCons: Constraint?
    
    var statusViewModel: XNStatusViewModel? {
        didSet {
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            
//            pictureView.pictures = statusViewModel?.status?.retweeted_status?.pic_urls
            
            //设置数据前根据图片的数量来判断是否需要调整自己底部的约束
            //在更新约束前需要先卸载之前的记录约束
            bottomCons?.uninstall()
            
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                pictureView.pictures = urls
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                pictureView.hidden = false
            } else {
                pictureView.pictures = nil
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                pictureView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        
        pictureView.snp_updateConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            make.left.equalTo(contentLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) in
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    //懒加载控件
    private lazy var contentLabel: UILabel = UILabel(title: "我是王", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    
    private lazy var pictureView: XNXNStatusPictureView = XNXNStatusPictureView()
}
